//
//  collectViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-12.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "collectViewController.h"
#import "collectionView.h"

#import "ArchiveHandleSingle.h"

#import "articleTableViewController.h"
#import "prictureTableViewController.h"

@interface collectViewController ()<UIScrollViewDelegate>

@property(nonatomic,retain)collectionView *collect;
@end

@implementation collectViewController

-(void)dealloc{
  
    [_collect release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.title = @"收藏";
        
        self.articleVC = [[articleTableViewController alloc] init];
        self.picVC = [[prictureTableViewController alloc] init];
        
        [self addChildViewController:self.articleVC];
        [self addChildViewController:self.picVC];
        
        //进去之前先进行反归档,看看沙盒里面有没有
        [[ArchiveHandleSingle archiveSingle] unachiveArray];
        
        self.collect = [[[collectionView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
        
        
    }
    return self;
}
-(void)loadView{

    self.view = self.collect;
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    //设置代理
    
    self.collect.collectionScrollV.delegate = self;
    //初始化一个右button
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style: UIBarButtonItemStyleDone target:self action:@selector(editAction:)];
    
    self.navigationItem.rightBarButtonItem = right;
    
    //初始化一个编辑按钮,并添加事件
    [self.collect.collectionSegment addTarget:self action:@selector(segAction:) forControlEvents: UIControlEventValueChanged];
    
    
 
}

//编辑按钮事件
-(void)editAction:(UIBarButtonItem *)sender{

    //点击删除按钮时给删除状态
    _style = UITableViewCellEditingStyleDelete;
   // static BOOL flag;
    for (int i= 1;i <= 2; i++) {
        UITableView *tab = (UITableView *)[self.collect.collectionScrollV viewWithTag:100 + i];
        
       BOOL flag = tab.editing;
        if (flag) {
          self.navigationItem.rightBarButtonItem.title = @"编辑";
        }else{
        
           self.navigationItem.rightBarButtonItem.title = @"完成";
        }
        [tab setEditing:!flag animated:YES];
       
    }
    
}

-(void)segAction:(UISegmentedControl *)sender{

    [UIView animateWithDuration:0 animations:^{
        self.collect.collectionScrollV.contentOffset=CGPointMake(sender.selectedSegmentIndex*[UIScreen mainScreen].bounds.size.width, 0);
    }];

}

//滑动scrollview时与segment联动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.collect.collectionSegment.selectedSegmentIndex=scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
